import { tujianType } from "./other/page/TujianPage";

export const LOCAL_STORAGE_VERSION = '1.0.0';


class gameData {
    key_game_data = "cyxx_game_data";
    gameStart = true;
    root = null;
    curLevelPrefab = null;
    getKey(key, isAppVersionData) {
        if (isAppVersionData) {
            return `${LOCAL_STORAGE_VERSION}-${key}`;
        } else {
            return key;
        }
    }
    getStorage(key, isAppVersionData = true) {
        let k = this.getKey(key, isAppVersionData);
        return cc.sys.localStorage.getItem(k);
    }
    setStorage(key, value, isAppVersionData = true) {
        let k = this.getKey(key, isAppVersionData);
        let v = value;
        cc.sys.localStorage.setItem(k, v);
    }

    getGameStart() {
        return this.gameStart;
    }
    setGameStart(start) {
        this.gameStart = start;
    }

    gameData: capitalDataType = null;
    getGameData() {
        return this.gameData;
    }

    gamePause = false;
    getGamePause() {
        return this.gamePause;
    }
    setGamePause(gamePause) {
        this.gamePause = gamePause;
    }

    setStorageGameData() {
        if (!this.gameStart) return;
        this.setStorage(this.key_game_data, JSON.stringify(this.gameData))
    }

    // 初始化
    init() {
        let datastring = this.getStorage(this.key_game_data);
        // cc.log("本地数据：", datastring);
        this.gameData = datastring && JSON.parse(datastring);
        // cc.log("本地数据gameData：", this.gameData);
        if (this.gameData) {
            this.gameData = {
                curLevel: this.gameData.curLevel == null ? 1 : this.gameData.curLevel,
                maxCurLevel: this.gameData.maxCurLevel == null ? 1 : this.gameData.maxCurLevel,
                isOpenXinShouTips: this.gameData.isOpenXinShouTips ? true : false,
                isTriggerXinShou: true,
            }
            this.gameStart = true;
            this.passAYearReLoadData();
        }
        else {
            let temp: capitalDataType = {
                curLevel: 1,
                maxCurLevel: 1,
                isOpenXinShouTips: true,
                isTriggerXinShou: false,
            }
            this.gameData = temp;
            // this.gameData = new capitalDataType(0, 0, gameConfig.MAX_POWER, 0, false, [0], false, 0, 0, 0, 0, 0, 0, 0);
            this.passAYearReLoadData();
        }
    }

    /** 过了一年后需要刷新的数据 */
    passAYearReLoadData() {

        this.setStorageGameData();
    }


    /*******************************  主玩法的参数   ***********************/


    getCurLevel() {
        return this.gameData.curLevel;
    }
    setCurLevel(curLevel: number) {
        this.gameData.curLevel = curLevel;
        this.setStorageGameData();
    }

    getIsOpenXinShouTips() {
        return this.gameData.isOpenXinShouTips;
    }
    setIsOpenXinShouTips(isOpen: boolean) {
        this.gameData.isOpenXinShouTips = isOpen;
        this.setStorageGameData();
    }

    getMaxCurLevel() {
        return this.gameData.maxCurLevel;
    }

    setMaxCurLevel(setMaxLevel: number) {
        this.gameData.maxCurLevel = setMaxLevel;
        this.setStorageGameData();
    }

    addMaxCurLevel() {
        this.gameData.maxCurLevel++;
        this.setStorageGameData();
    }

    getIsTriggerXinShou() {
        return this.gameData.isTriggerXinShou;
    }

    setIsTriggerXinShou(isTrigger: boolean) {
        this.gameData.isTriggerXinShou = isTrigger;
        this.setStorageGameData();
    }

}
export const GameDataHelp = new gameData();



interface capitalDataType {
    curLevel: number,  // 当前所处的关卡
    maxCurLevel: number, // 当前所通过的关卡数 -- 会一直累加

    /** 是否是第一次进入游戏 */
    isOpenXinShouTips: boolean,

    /** 是否触发新手引导 */
    isTriggerXinShou: boolean,
}
